<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hddf.project.pojos.Project"%>
<%@page import="com.hddf.project.pojos.ProjectPlan"%>
<%@page import="com.hddf.project.service.PlanService"%>
<%@page import="com.hddf.project.service.ExportExcelService"%>
<%@page import="com.hddf.project.util.O"%>
<%@page import="java.io.OutputStream"%>
<%@page import="com.hddf.project.interceptor.CookieService"%>
<%@page import="com.hddf.project.interceptor.GetData"%>
<%@page import="com.hddf.project.action.PlanAction"%>

<!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01 TransitionalEN">
<html>
<head>
<title>导出Excel</title>
<script type="text/javascript" src="/js/global_param.js"></script>
</head>
<%
	CookieService cs = CookieService.getInstance();
	Cookie ctoken = cs.getCookieByName(request, "to");
	Cookie ctime = cs.getCookieByName(request, "ti");
	boolean b = (null != ctime && !"".equals(ctime.getValue()));
	boolean b2 = (null != ctoken && !"".equals(ctoken.getValue()));
	if (b && b2) {
		String web_token = ctoken.getValue();
		String web_time = ctime.getValue();

		String uri = request.getRequestURI();
		if (uri.indexOf("?") != -1) {
			uri = uri.substring(uri.lastIndexOf("/") + 1, uri.indexOf("?"));
		} else {
			uri = uri.substring(uri.lastIndexOf("/") + 1);
		}
		GetData gd = new GetData();
		Map<String, Object> m = gd.checkPermission(web_token, web_time, uri);

		String str = (String) m.get("msg");
		String permission = (String) m.get("permission");
		String state = (String) m.get("state");
		if (("".equals(str) || null == str || 0 == str.length()) && state.equals("success")) {
			if (permission.equals("ok")) {
				Object _planId = request.getParameter("planId");
				if (null != _planId) {
					int planId = Integer.valueOf(_planId.toString());
					PlanService ps = PlanService.getInService();
					ProjectPlan pp = ps.getPlanAndCountByPlanId(planId);
					if (null != pp) {
						List<Project> pList = ps.planExcelByPlanId(planId);
						pp.setpList(pList);

						String time = O.getTimestamp3();
						time = time.replaceAll(" ", "_");
						
						String fileName = pp.getName() + "（" + pp.getCountProject() + "项）" + "_" + time + ".xls";
						fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
						OutputStream os = response.getOutputStream(); //取得输出流
						response.reset(); //清空输出流
						response.setContentType("application/msexcel");
						response.setHeader("Content-disposition", "attachment; filename=" + fileName);

						ExportExcelService ees = ExportExcelService.getInstance();
						ees.planExcelByPlanId(os, pp);
					}
				}
			} else {
				%>
				<script type="text/javascript">
					alert("您没有该操作的权限！");
					window.close();
				</script>
				<%
			}
		} else {
			%>
			<script type="text/javascript">
				alert(str);
			</script>
			<%
		}
	} else {
		%>
		<script type="text/javascript">
			alert("请您登录！");
			window.location.href=global_parameter.login_page;
		</script>
		<%
	}
%>
<body>
</body>
</html>